package com.gtja.beoms.utils;

import com.alibaba.excel.EasyExcel;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;

/**
 * 生成Excel的工具类
 * @author Ghao
 * @date 2021/11/24
 */
public class OfficeUtil {

    /**
     * web下载excel
     * @param response response
     * @param clazz 行对应的类
     * @param list 数据
     * @throws IOException
     */
    private void generateExcel(HttpServletResponse response, Class clazz, ArrayList<Object> list) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), clazz).sheet("模板").doWrite(list);
    }
}
